﻿using System.Data;
using System.IO;
using DM4;
using System;
using System.ServiceModel;

namespace App_Code
{
    public static class CommonProc
    {
        public static bool CreateServerChannel(string endpointAddress)
        {
            try
            {
                CommonVars.Dm4ChannelFactory = new ChannelFactory<IDM4>(
                    new BasicHttpBinding(),
                    new EndpointAddress(endpointAddress));

                CommonVars.Dm4Srv = CommonVars.Dm4ChannelFactory.CreateChannel();
            }
            catch (Exception ex)
            {
                SetLastError(StringMessages.ErrorOfCreateServerChannel + Environment.NewLine + ex.Message);
                return false;
            }

            return true;
        }

        public static bool ServerLogin(string login, string password)
        {
            XMLInfo xi;
            DataTable table;
            try
            {
                xi = CommonVars.Dm4Srv.Login(login, password);
                table = XMLInfo.FillTable(xi);                
            }
            catch (Exception ex)
            {
                SetLastError(ex.Message);
                return false; throw;
            }

            return true;
            //try
            //{
            //    CommonVars.Ui = CommonVars.Dm4Srv.Login(login, password);
            //    if (CommonVars.Ui.Id == DM4Consts.ErrorUnknown)
            //    {
            //        CommonVars.LastError = StringMessages.InvalidUserPass;
            //        return false;
            //    }

            //    //fill common variables
            //    CommonVars.Gi = CommonVars.Dm4Srv.GetInfo();
            //    CommonVars.Uil =
            //        CommonVars.Ui.AccessLevel == AccessLevel.Admin
            //        ? CommonVars.Dm4Srv.GetUserList()
            //        : null;

            //    CommonVars.Uil = CommonVars.Dm4Srv.GetUserList();
            //}
            //catch (Exception ex)
            //{
            //    SetLastError(ex.Message);
            //    return false;
            //}
            //return true;
        }

        public static bool RefreshUserList()
        {
            //try
            //{
            //    CommonVars.Uil = null;
            //    CommonVars.Uil =
            //       CommonVars.Ui.AccessLevel == AccessLevel.Admin
            //       ? CommonVars.Dm4Srv.GetUserList()
            //       : null;
            //}
            //catch (Exception ex)
            //{
            //    SetLastError(ex.Message);
            //    return false;
            //}
            return true;
        }

        public static void SetLastError(string message, bool isClearPrevious = false)
        {
            if (String.IsNullOrEmpty(message)) return;

            if (isClearPrevious)
                CommonVars.LastError = "";

            if (!String.IsNullOrEmpty(CommonVars.LastError))
                CommonVars.LastError += Environment.NewLine;

            CommonVars.LastError += message;
        }

        public static bool GetGroupList(int userId)
        {
            try
            {
                //CommonVars.Gil = CommonVars.Dm4Srv.GetGroupList(userId);
                if (CommonVars.GroupInfods == null)
                    InitDs();
                if (CommonVars.GroupInfods == null)
                    return false;

                CommonVars.GroupInfods.Tables["tbl"].Rows.Clear();
                foreach (GroupInfo gi in CommonVars.Gil)
                {
                    DataRow dr = CommonVars.GroupInfods.Tables["tbl"].NewRow();
                    dr["Disabled"] = gi.Disabled;
                    dr["Name"] = gi.Name;
                    CommonVars.GroupInfods.Tables["tbl"].Rows.Add(dr);
                }

                //if (CommonVars.GroupInfods.Tables["tbl"].Rows.Count == 0)
                {
                    DataRow dr = CommonVars.GroupInfods.Tables["tbl"].NewRow();
                    dr["Disabled"] = true;
                    dr["Name"] = "TEST NAME";
                    CommonVars.GroupInfods.Tables["tbl"].Rows.Add(dr);
                }
                {
                    DataRow dr = CommonVars.GroupInfods.Tables["tbl"].NewRow();
                    dr["Disabled"] = true;
                    dr["Name"] = "TEST NAME";
                    CommonVars.GroupInfods.Tables["tbl"].Rows.Add(dr);
                }
                {
                    DataRow dr = CommonVars.GroupInfods.Tables["tbl"].NewRow();
                    dr["Disabled"] = true;
                    dr["Name"] = "TEST NAME";
                    CommonVars.GroupInfods.Tables["tbl"].Rows.Add(dr);
                }
                {
                    DataRow dr = CommonVars.GroupInfods.Tables["tbl"].NewRow();
                    dr["Disabled"] = true;
                    dr["Name"] = "TEST NAME";
                    CommonVars.GroupInfods.Tables["tbl"].Rows.Add(dr);
                }
                {
                    DataRow dr = CommonVars.GroupInfods.Tables["tbl"].NewRow();
                    dr["Disabled"] = true;
                    dr["Name"] = "TEST NAME";
                    CommonVars.GroupInfods.Tables["tbl"].Rows.Add(dr);
                }
                {
                    DataRow dr = CommonVars.GroupInfods.Tables["tbl"].NewRow();
                    dr["Disabled"] = true;
                    dr["Name"] = "TEST NAME";
                    CommonVars.GroupInfods.Tables["tbl"].Rows.Add(dr);
                }
                {
                    DataRow dr = CommonVars.GroupInfods.Tables["tbl"].NewRow();
                    dr["Disabled"] = true;
                    dr["Name"] = "TEST NAME";
                    CommonVars.GroupInfods.Tables["tbl"].Rows.Add(dr);
                }
                {
                    DataRow dr = CommonVars.GroupInfods.Tables["tbl"].NewRow();
                    dr["Disabled"] = true;
                    dr["Name"] = "TEST NAME";
                    CommonVars.GroupInfods.Tables["tbl"].Rows.Add(dr);
                }
                {
                    DataRow dr = CommonVars.GroupInfods.Tables["tbl"].NewRow();
                    dr["Disabled"] = true;
                    dr["Name"] = "TEST NAME";
                    CommonVars.GroupInfods.Tables["tbl"].Rows.Add(dr);
                }
                {
                    DataRow dr = CommonVars.GroupInfods.Tables["tbl"].NewRow();
                    dr["Disabled"] = true;
                    dr["Name"] = "TEST NAME";
                    CommonVars.GroupInfods.Tables["tbl"].Rows.Add(dr);
                }
                {
                    DataRow dr = CommonVars.GroupInfods.Tables["tbl"].NewRow();
                    dr["Disabled"] = true;
                    dr["Name"] = "TEST NAME";
                    CommonVars.GroupInfods.Tables["tbl"].Rows.Add(dr);
                }
                {
                    DataRow dr = CommonVars.GroupInfods.Tables["tbl"].NewRow();
                    dr["Disabled"] = true;
                    dr["Name"] = "TEST NAME";
                    CommonVars.GroupInfods.Tables["tbl"].Rows.Add(dr);
                }
            }
            catch (Exception ex)
            {
                SetLastError(ex.Message);
                return false;
            }
            return true;
        }

        public static void InitDs()
        {
            try
            {
                CommonVars.GroupInfods = new DataSet("ds");
                DataTable tbl = new DataTable("tbl");
                CommonVars.GroupInfods.Tables.Add(tbl);
                DataColumn dt = new DataColumn
                                    {
                                        DataType = Type.GetType("System.Boolean"),
                                        ColumnName = "Disabled",
                                        Caption = "Disabled",

                                        ReadOnly = false,
                                        DefaultValue = 1
                                    };

                CommonVars.GroupInfods.Tables["tbl"].Columns.Add(dt);

                dt = new DataColumn
                         {
                             DataType = Type.GetType("System.String"),
                             ColumnName = "Name",
                             Caption = "Name",
                             ReadOnly = false,
                             DefaultValue = ""
                         };

                CommonVars.GroupInfods.Tables["tbl"].Columns.Add(dt);

                dt = new DataColumn
                {
                    DataType = Type.GetType("System.Int32"),
                    ColumnName = "Id",
                    Caption = "Id",
                    AutoIncrement = true,
                    AutoIncrementSeed = 1,
                    AutoIncrementStep = 2
                };
                dt.Unique = true;

                CommonVars.GroupInfods.Tables["tbl"].Columns.Add(dt);


            }
            catch (Exception ex)
            {

                throw;
            }
        }
    }
}